class Tag < ActiveRecord::Base
  has_and_belongs_to_many :photos
  
  
   
  def self.get_photos(tagname)
  tagname = tagname.strip
    tag=Tag.find_by_title(tagname);
    if tag
      tag.photos
    end
  end
 
  def self.edit_tag(tagname,newtagname)
  tagname = tagname.strip
  newtagname = newtagname.strip
    tag=Tag.find_by_title(tagname)
    if tag
      tag.title="#{newtagname}"
     tag.save
    end
  end
   
  def self.get_tag_weight(tagname)
  tagname = tagname.strip
    tag=Tag.find_by_title(tagname)
    if tag
      tag.photos.size 
    end   
  end
    
  def self.get_tags_with_weight(weight)
    @alltags=Tag.find :all
    @tags=Array.new
    @alltags.each do |t|
      if t.photos.size==weight
        @tags<<t
      end
    end
    @tags  
  end
  
  def self.get_all_tags
    @alltags=Tag.find :all
    @alltags
  end
   
  def self.get_linked(tagname)
  tagname = tagname.strip
    tag=Tag.find_by_title(tagname)
    if tag
      @tags=Array.new
      @counters=Hash.new
      @tags<<tag
      @counters[tag]=0
      tag.photos.each do |p|
        p.tags.each do |t|
          if !(@tags.include?(t))
            @tags<<t
            @counters[t]=1
          else
            @counters[t]+=1
          end
        end
      end
      @tags.sort_by { |t| @counters[t] }
      @tags.delete(tag)
      @tags
    end   
  end
  
end

